package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Event;
import entities.User;

import managers.manEvent;

/**
 * Servlet implementation class GetMyEvents
 */
public class GetMyEvents extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	manEvent manE;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMyEvents() {
        super();
        // TODO Auto-generated constructor stub
        manE=new manEvent();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		
		
		String types=(String)request.getParameter("types");
		String id=(String)request.getParameter("idUser");	
		ArrayList<Event> allEvent = new ArrayList<Event>();
		if("MyFutureEvents".equals(types)){
			User pat=manE.searchById(id);
			Iterator<Event> it=pat.getParEvents().iterator();
	    	while(it.hasNext()){
	    		allEvent.add(it.next()) ;
	    	}
			
			request.setAttribute("allEvent", allEvent);
			request.setAttribute("type", "MyFutureEvents");
			request.getRequestDispatcher("/allEvent.jsp").forward(request, response);
		}
		if("MyOrganisedEvents".equals(types)){
			User pat=manE.searchById(id);
			Iterator<Event> it=pat.getEvents().iterator();
			
	    	while(it.hasNext()){
	    		allEvent.add(it.next());
	    	}
			
			request.setAttribute("allEvent", allEvent);
			request.setAttribute("type", "MyOrganisedEvents");
			request.getRequestDispatcher("/allEvent.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
